import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  OneToOne,
  ManyToOne,
  OneToMany,
} from "typeorm";
import { UserExtend } from "./UserExtend";
import { Posts } from "./Posts";

// entity 是由@Entity装饰器装饰的模型，将为此类创建数据库表
@Entity({ name: "user" })
export class User {
  // 下面定义的会自动在数据库生成对应的字段
  // 主键是自动自增的
  @PrimaryGeneratedColumn({
    type: "int",
    name: "id",
    comment: "主键id",
  })
  id: number;

  @Column({
    type: "varchar",
    nullable: false,
    length: 50,
    unique: true,
    name: "username", // 如果是一样的可以不指定
    comment: "用户名",
  })
  username: string;

  @Column({
    type: "varchar",
    nullable: false,
    length: 100,
    comment: "密码",
  })
  password: string;

  @Column("tinyint", {
    nullable: false,
    default: () => 0,
    name: "is_del",
    comment: "是否删除，1表示删除，0表示正常",
  })
  isDel: number;

  @CreateDateColumn({
    type: "timestamp",
    nullable: false,
    name: "created_at",
    comment: "创建时间",
  })
  createdAt: Date;

  @UpdateDateColumn({
    type: "timestamp",
    nullable: false,
    name: "updated_at",
    comment: "更新时间",
  })
  updateAt: Date;

  // 一对一
  @OneToOne((type) => UserExtend, (userExtend) => userExtend.user)
  userDetail: UserExtend;

  // 一对多  一个人有多个帖子
  @OneToMany((type) => Posts, (post) => post.user)
  posts: Posts[];
}
